﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
[CustomEditor(typeof(UISelectObjectPath), true)]
public class UISelectObjectPathInspector : Editor
{
    UISelectObjectPath mTarget;
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        mTarget = target as UISelectObjectPath;
        GUILayout.Space(3f);
        if (NGUIEditorTools.DrawHeader("Select Object",mTarget.GetInstanceID().ToString()))
        {
            NGUIEditorTools.BeginContents();
            mTarget.selectType = (UISelectObjectPath.SelectType)EditorGUILayout.EnumPopup("Select Type", mTarget.selectType);
            if (mTarget.selectType == UISelectObjectPath.SelectType.selectByPath && mTarget.IsOpenABLoader())
            {
                mTarget.outsideABObject = EditorGUILayout.Toggle("Load outside AB", mTarget.outsideABObject);
            }

            switch(mTarget.selectType)
            {
                case UISelectObjectPath.SelectType.selectByPath:
                    if(!mTarget.outsideABObject)
                    {
                        mTarget.selectObj = null;
                        Object obj = null;
                        obj = EditorGUILayout.ObjectField("Select Object", obj, typeof(Object), false);
                        string temPath = "";
                        if (null != obj)
                        {
                            temPath = AssetDatabase.GetAssetPath(obj);
                            if (temPath.Contains("/" + CommonConst.RESOURCE_NAME + "/") ||
                                temPath.Contains("/" + CommonConst.RESOURCE_NAME.ToLower() + "/"))
                            {
                                mTarget.objectPath = temPath;
                                mTarget.objectGUID = AssetDatabase.AssetPathToGUID(temPath);
                                mTarget.canLoadForScene = obj is GameObject;
                            }
                            else
                            {
#if USE_LOGWRAPPER 
 LogWrapper.LogError("Can't load this path resources!");  
#endif
                            }
                            EditorGUILayout.LabelField("Path", mTarget.objectPath);
                            obj = null;
                        }
                    }
                    else
                    {
                        mTarget.selectObj = null;
                        if( GUILayout.Button("Select ouside AB Object"))
                        {
                            LogWrapper.LogError("...path", Application.dataPath);
                            string abSelectPath = EditorUtility.OpenFilePanel("选择AB文件", "", "");
                            //LogWrapper.LogError("select path:", abSelectPath, "xxxxxxxxx", mTarget.GetABPathBySelectPath(abSelectPath));
                            mTarget.objectPath = mTarget.GetABPathBySelectPath(abSelectPath);
                            mTarget.objectCDPath = abSelectPath;
                            //mTarget.objectGUID = "";AssetDatabase.AssetPathToGUID(temPath);
                            mTarget.canLoadForScene = true;
                        }
                    }


                    EditorGUILayout.LabelField("Path", mTarget.objectPath);
                    if(mTarget.canLoadForScene)
                    {
                        mTarget.loadToType = (UISelectObjectPath.LoadToType)EditorGUILayout.EnumPopup("Load To Add", mTarget.loadToType);
                        switch(mTarget.loadToType)
                        {
                            case UISelectObjectPath.LoadToType.target:
                                mTarget.targetGameObject = EditorGUILayout.ObjectField("Load To", mTarget.targetGameObject, typeof(GameObject)) as GameObject;
                                break;
                            case UISelectObjectPath.LoadToType.self:
                            case UISelectObjectPath.LoadToType.world:
                                mTarget.targetGameObject = null;
                                break;
                        }
                        if (mTarget.loadToType != UISelectObjectPath.LoadToType.self)
                        {
                            mTarget.autoContact = EditorGUILayout.Toggle("Auto Contack", mTarget.autoContact);
                        }
                    }
                    if (!AssetDatabase.GUIDToAssetPath(mTarget.objectGUID).Equals(mTarget.objectPath)&&!mTarget.outsideABObject)
                    {
                        EditorGUILayout.HelpBox("this Asset may be moved!", MessageType.Error);
                        GUI.backgroundColor = Color.green;
                        if (GUILayout.Button("Correct By Path To GUID", GUILayout.MinWidth(100f)))
                        {
                            #if USE_LOGWRAPPER 
 LogWrapper.LogError("====" , AssetDatabase.LoadAssetAtPath(mTarget.objectPath, typeof(Object)));  
 #endif
                            if (null != AssetDatabase.LoadAssetAtPath(mTarget.objectPath, typeof(Object)))
                                mTarget.objectGUID = AssetDatabase.AssetPathToGUID(mTarget.objectPath);
                            else
                                #if USE_LOGWRAPPER 
 LogWrapper.LogError("this Asset may be miss or Path is Error!");  
 #endif
                            Repaint();
                        }
                        if (GUILayout.Button("Correct By GUID TO Path", GUILayout.MinWidth(100f)))
                        {
                            if (AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(mTarget.objectGUID), typeof(Object)))
                                mTarget.objectPath = AssetDatabase.GUIDToAssetPath(mTarget.objectGUID);
                            else
                                #if USE_LOGWRAPPER 
 LogWrapper.LogError("this Asset may be miss or Saved GUID is Error!");  
 #endif
                            Repaint();
                        }
                        GUI.backgroundColor = Color.white;
                    }
                    else
                    {
                        if(!string.IsNullOrEmpty(mTarget.objectPath)&& (!string.IsNullOrEmpty(mTarget.objectGUID)||mTarget.outsideABObject))
                        {
                            GUI.backgroundColor = Color.green;
                            if (GUILayout.Button("Select Object"))
                            {
                                if(mTarget.outsideABObject)
                                {
                                    int mTargetNameIndex = mTarget.objectCDPath.LastIndexOf("/");
                                    string path = mTarget.objectCDPath.Substring(0, mTargetNameIndex + 1);
                                    System.Diagnostics.Process.Start(path);
                                }
                                else
                                {
                                    SelectLoadObject(AssetDatabase.GUIDToAssetPath(mTarget.objectGUID));
                                }
                                
                                //Selection.activeObject = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(mTarget.objectGUID), typeof(Object));
                            }
                            if (mTarget.canLoadForScene)
                            {
                                GUI.backgroundColor = Color.yellow;
                                if (GUILayout.Button("Load Object"))
                                {
                                    LoadResHandler();
                                }
                                GUI.backgroundColor = Color.red;
                                if (GUILayout.Button("Clear Object"))
                                {
                                    mTarget.Clear();
                                }
                            }
                            GUI.backgroundColor = Color.white;
                        }
                    }
                    break;
                case UISelectObjectPath.SelectType.selectByObject:
                    mTarget.objectPath = "";
                    mTarget.objectGUID = "";
                    mTarget.selectObj = EditorGUILayout.ObjectField("Select Object", mTarget.selectObj, typeof(Object), false);

                    if (mTarget.canLoadForScene)
                    {
                        mTarget.loadToType = (UISelectObjectPath.LoadToType)EditorGUILayout.EnumPopup("Load To Add", mTarget.loadToType);
                        switch (mTarget.loadToType)
                        {
                            case UISelectObjectPath.LoadToType.target:
                                mTarget.targetGameObject = EditorGUILayout.ObjectField("Load To", mTarget.targetGameObject, typeof(GameObject)) as GameObject;
                                break;
                            case UISelectObjectPath.LoadToType.self:
                            case UISelectObjectPath.LoadToType.world:
                                mTarget.targetGameObject = null;
                                break;
                        }
                        if(mTarget.loadToType != UISelectObjectPath.LoadToType.self)
                        {
                            mTarget.autoContact = EditorGUILayout.Toggle("Auto Contack", mTarget.autoContact);
                        }
                    }

                    GUI.backgroundColor = Color.green;
                    if (GUILayout.Button("Select Object"))
                    {
                        EditorGUIUtility.PingObject(mTarget.selectObj);
                        //Selection.activeObject = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(mTarget.objectGUID), typeof(Object));
                    }
                    if (mTarget.selectObj is GameObject)
                    {
                        GUI.backgroundColor = Color.yellow;
                        if (GUILayout.Button("Load Object"))
                        {
                            LoadResHandler();
                        }
                        GUI.backgroundColor = Color.red;
                        if (GUILayout.Button("Clear Object"))
                        {
                            mTarget.Clear();
                        }
                    }
                    GUI.backgroundColor = Color.white;
                    break;
            }
            
            NGUIEditorTools.EndContents();
        }
    }
    void LoadResHandler()
    {
        MethodInfo tMethod = mTarget.GetType().GetMethod("SpawnOneObject",
          BindingFlags.NonPublic | BindingFlags.Instance,
          null,
          new System.Type[] { typeof(string) },
          null);
        if (tMethod != null)
            tMethod.Invoke(mTarget, new object[] { "loadResItem" });
    }
    public void SelectLoadObject(string path)
    {
        Object o = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
        if(null!=o)
            EditorGUIUtility.PingObject(o);
        else
        {
#if USE_LOGWRAPPER 
 LogWrapper.LogError("can not load this object!");  
#endif
        }
            
    }
}

